package com.aaa.operations.config;


import com.aaa.operations.util.MyResidCache;
import com.aaa.operations.util.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author ：陈路遥
 * @date ：Created in 2020/7/9 15:10
 * @description：Redis的工厂，创建Redis的工厂。
 * 第一步：拿到RedisPropertiesConfig类，里面存放redis的所有连接ip和端口号。返回RedisClusterConfiguration类
 * 第二步：拿到连接池。JedisPoolConfig是jedis(java为redis创建的api)的连接池，底层类中有默认数据。也可以自己设置数据
 * 第三步：把连接池和redis的ip端口号作为参数传给JedisConnectionFactory的构造器
 * 第四步：把获取JedisConnectionFactory的对象传值给实现了achce接口的类，即可操作redis
 * 其他类可以创建redis的connection连接对象
 * @modified By：
 * @version:
 */

@Configuration
public class RedisFactory {
    //第一步：获取RedisPropertiesConfig工具类，里面存所有ip以及端口号
    @Autowired
    private RedisProperties redisPropertiesConfig;

    @Bean
    public RedisClusterConfiguration getRedisClusterConfiguration(){
        RedisClusterConfiguration redisClusterConfiguration =
                new RedisClusterConfiguration();
        String nodes = redisPropertiesConfig.getNodes();
        String[] nodesArray = nodes.split(",");
        for (String nodeArray : nodesArray) {
            String[] node = nodeArray.split(":");
            System.out.println(node);
            RedisNode redisNode = new RedisNode(node[0],Integer.parseInt(node[1]));
            //获取redis的ip和端口
            redisClusterConfiguration.addClusterNode(redisNode);
        }
        return redisClusterConfiguration;
    }

    //第二步：拿到连接池JedisPoolConfig，因为连接池有默认数据，所以直接获取对象即可
    @Bean
    public JedisPoolConfig getJedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        return jedisPoolConfig;
    }

    //第三步：把连接池和redis的ip端口号作为参数传给JedisConnectionFactory的构造器
    @Bean
    public JedisConnectionFactory getJedisConnectionFactory(){
        JedisConnectionFactory jedisConnectionFactory =
                new JedisConnectionFactory(getRedisClusterConfiguration(),getJedisPoolConfig());
        return jedisConnectionFactory;
    }

    // * 第四步：把获取JedisConnectionFactory的对象传值给实现了achce接口的类
    @Bean
    public String redisFavtoryConnection(){
        MyResidCache.getJedisConnectionFactory(getJedisConnectionFactory());
        return null;
    }


    /*@Bean
    public JedisCluster test2(){
        Set set = new HashSet();
        String nodes = redisPropertiesConfig.getNodes();
        String[] nodesArray = nodes.split(",");
        for (String nodeArray : nodesArray) {
            String[] node = nodeArray.split(":");
            HostAndPort hostAndPort = new HostAndPort(node[0],Integer.parseInt(node[1]));
            set.add(hostAndPort);
        }
        //连接集群
        JedisCluster jedisCluster = new JedisCluster(set);
        jedisCluster.set("aaa","111");

        return jedisCluster;
    }*/
}
